package com.example.homework.fragment;

import android.media.MediaPlayer;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.homework.R;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link MusicFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class MusicFragment extends Fragment  implements View.OnClickListener{

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private ImageView disc,discB,pre,next,play;
    private Button shunxu,suiji,only;
    private TextView title,author;
    private int[] music = {R.raw.a,R.raw.b};
    private String[] Stitle = {"找自己","怪天气"};
    private String[] Sauther = {"陶喆","黄萱",};
    private int[] photo = {R.drawable.a,R.drawable.b};
    private int i;
    private boolean flag = false;
    private MediaPlayer mediaPlayer;

    public MusicFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment MusicFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static MusicFragment newInstance(String param1, String param2) {
        MusicFragment fragment = new MusicFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        final View view = inflater.inflate(R.layout.fragment_music, container, false);
        disc = view.findViewById(R.id.disc);
        discB = view.findViewById(R.id.disc_c);
        pre = view.findViewById(R.id.playing_pre);
        next = view.findViewById(R.id.playing_next);
        play = view.findViewById(R.id.playing_play);
        title = view.findViewById(R.id.title);
        author = view.findViewById(R.id.action);
        shunxu=view.findViewById(R.id.shunxu);
        suiji=view.findViewById(R.id.suiji);
        only=view.findViewById(R.id.only);
        Animation animation = AnimationUtils.loadAnimation(getContext(), R.anim.img_rotate);
        LinearInterpolator lin = new LinearInterpolator();
        discB.startAnimation(animation);
        set();
        Toast.makeText(getContext(), "当前检测到"+music.length, Toast.LENGTH_SHORT).show();
        play.setOnClickListener(this);
        next.setOnClickListener(this);
        pre.setOnClickListener(this);
        shunxu.setOnClickListener(this);
        suiji.setOnClickListener(this);
        only.setOnClickListener(this);
        return view;
    }
    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.playing_pre:
                if(i == 0){
                    Toast.makeText(getContext(), "已经到第一首了哦", Toast.LENGTH_SHORT).show();
                }else {
                    mediaPlayer.pause();
                    i -= 1;
                    if(!flag){
                        flag = true;
                        play.setImageResource(R.drawable.play);
                        set();
                        play();
                    }else {
                        set();
                        play();
                    }
                }
                break;
            case R.id.playing_next:
                if(i == music.length - 1){
                    mediaPlayer.pause();
                    i = 0;
                    set();
                    play();
                }else {
                    mediaPlayer.pause();
                    i += 1;
                    if(!flag){
                        flag = true;
                        play.setImageResource(R.drawable.play);
                        set();
                        play();
                    }else {
                        set();
                        play();
                    }
                }
                break;
            case R.id.playing_play:
                if(flag) {
                    flag = false;
                    play.setImageResource(R.drawable.pause);
                    mediaPlayer.pause();
                }else {
                    flag = true;
                    play.setImageResource(R.drawable.play);
                    mediaPlayer.start();
                    play();
                }
                break;
            case R.id.shunxu:
                Toast.makeText(getContext(),"当前沿列表顺序播放",Toast.LENGTH_SHORT).show();
                break;
            case R.id.suiji:
                Toast.makeText(getContext(),"当前按列表随机播放",Toast.LENGTH_SHORT).show();
                break;
            case R.id.only:
                Toast.makeText(getContext(),"当前为单曲循环",Toast.LENGTH_SHORT).show();
                break;
        }
    }
    public void set(){
        title.setText(Stitle[i]);
        author.setText(Sauther[i]);
        disc.setImageResource(photo[i]);
        mediaPlayer = MediaPlayer.create(getContext(),music[i]);
    }

    public void play(){
        try {
            mediaPlayer.setOnPreparedListener(preparedListener);
            mediaPlayer.prepareAsync();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    MediaPlayer.OnPreparedListener preparedListener = new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            mediaPlayer.start();
        }
    };
}